package com.judian.smart.factory;

import com.judian.smart.model.ModeList;
import com.judian.smart.proxy.WidgetDAOProxy;

import java.util.ArrayList;
import java.util.List;

public class ModeListFactory {

    private volatile static ModeListFactory install;

    public static ModeListFactory getInstance() {
        if (null == install) {
            install = new ModeListFactory();
        }
        if (list.size() == 0) {
            list = WidgetDAOProxy.getInstance().getModeList();
        }
        return install;
    }

    private static List<ModeList> list = new ArrayList<>();


    public List<ModeList> getAll() {
        if (list.size() == 0) {
            list = WidgetDAOProxy.getInstance().getModeList();
        }
        return list;
    }

    public static List<ModeList> getModeDevice(int modeId) {
        List<ModeList> resultList = new ArrayList<ModeList>();
        if (list.size() == 0) {
            list = WidgetDAOProxy.getInstance().getModeList();
        }
        for (ModeList modeList : list) {
            if (modeList.getModeId() == modeId) {
                resultList.add(modeList);
            }

        }
        return resultList;
    }

    public ModeList getModeList(int Id) {
        ModeList modeList = null;
        if (list.size() == 0) {
            list = WidgetDAOProxy.getInstance().getModeList();
        }
        for (ModeList _modeList : list) {
            if (_modeList.getId() == Id) {
                modeList = _modeList;
                break;
            }

        }
        return modeList;
    }

    public void delete(int id) {

        for (ModeList modeList : list) {
            if (modeList.getId() == id) {
                list.remove(modeList);
                break;
            }

        }
    }

    public void clearList() {
        list = WidgetDAOProxy.getInstance().getModeList();
    }

}
